import { getGuid } from '../utils';

export default class Actor{
    constructor(name, player) {
        this.name = name;
        this.player = player;
        this.schedulers = [];
    }

    addSchedule(action, delay) {
        const uid = getGuid();
        this.schedulers.push({
            id: uid,
            action,
            delay,
        });
        return uid;
    }

    removeSchedule(id) {
        const scheduleIndex = this.schedulers.findIndex(vo => vo.id === id);
        if (scheduleIndex >= 0) {
            this.schedulers.splice(scheduleIndex, 1);
        }
    }

    play() {
        const actionPromise = this.schedulers.map(vo => {
           return new Promise((resolve, reject) => {
               vo.action.onStop = (node) => {
                   resolve(node);
               }
              setTimeout(() => {
                  this.player.runAction(vo.action);
              }, vo.delay);
           });
        });
        return Promise.all(actionPromise);
    }
}
